package titt.service.content;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import eu.medsea.mimeutil.MimeType;
import eu.medsea.mimeutil.detector.ExtensionMimeDetector;
import eu.medsea.mimeutil.detector.MimeDetector;

public class Mimes {

	public final static String 	HTML = "text/html", 
								XML = "text/xml", 
								PLAIN = "text/plain",
								CSS = "text/css",
								JAVA = "text/plain",
								JAVASCRIPT = "text/javascript",
								PNG = "image/png",
								JPEG = "image/jpeg",
								GIF = "image/gif",
								JAR = "application/java-archive",
								MP4 = "video/mp4",
								OGG = "video/ogg",
								M3U = "audio/x-mpegurl",
								DIVX = "video/x-divx",
								OCTET_STREAM = "application/octet-stream";

	private final static Map<String, String> mimes = new HashMap<String, String>();
	private final static MimeDetector md = new ExtensionMimeDetector();
	
	static {
		mimes.put("html",	HTML);
		mimes.put("xml",	XML);
		mimes.put("txt",	PLAIN);
		mimes.put("css",	CSS);
		mimes.put("js",		JAVASCRIPT);
		mimes.put("java", 	JAVA);
		mimes.put("png",	PNG);
		mimes.put("jpg", 	JPEG);
		mimes.put("jpeg", 	JPEG);
		mimes.put("gif", 	GIF);
		mimes.put("jar",	JAR);
		mimes.put("mp4",	MP4);
		mimes.put("ogv",	OGG);
		mimes.put("m3u",	M3U);
		mimes.put("avi", 	DIVX);
	}
	
	@SuppressWarnings("unchecked")
	public static String getContentType(String filename) {
		String[] tmp = filename.toLowerCase().split("\\.");
		String name = tmp[tmp.length-1];
		
		if(mimes.containsKey(name))
			return mimes.get(name);
		
		Collection<MimeType> ms = md.getMimeTypes(filename);
		
		
		if(!ms.isEmpty()) {
			MimeType m = ms.iterator().next();
			return m.toString();
		}
		
		return OCTET_STREAM;
		
	}


}
